package com.atguigu.edu.realtime.publisher.controller;

import com.atguigu.edu.realtime.publisher.bean.TradeCt;
import com.atguigu.edu.realtime.publisher.service.CourseTradeCountService;
import com.atguigu.edu.realtime.publisher.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CourseTradeCountController {
    @Autowired
    CourseTradeCountService courseTradeCountService;

    @RequestMapping("/subject")
    public String getSubjectOrderCnt(@RequestParam(value = "date", defaultValue = "1")Integer date){
        if(date == 1){
            date = DateUtil.now();
        }
        List<TradeCt> subjectOrderCnt = courseTradeCountService.getSubjectOrderCnt(date);
        StringBuilder sb = new StringBuilder("{\"status\": 0,\"msg\": \"\",\"data\": [");
        for (int i = 0; i< subjectOrderCnt.size() ;i++){
            TradeCt tradeCt = subjectOrderCnt.get(i);
            sb.append("{ \"name\": \""+ tradeCt.getName() +"\", \"value\": \""+tradeCt.getOrderAmount()+"\" }");
            if(i< subjectOrderCnt.size() -1){
                sb.append(",");
            }
        }
        sb.append(" ]}");
        return sb.toString();
    }

    @RequestMapping("/course")
    public String getCourseOrderCnt(@RequestParam(value = "date", defaultValue = "1")Integer date){
        if(date == 1){
            date = DateUtil.now();
        }
        List<TradeCt> courseOrderCnt = courseTradeCountService.getCourseOrderCnt(date);
        StringBuilder sb = new StringBuilder("  {\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"页面\",\n" +
                "        \"id\": \"page_id\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"下单人数\",\n" +
                "        \"id\": \"u_ct\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"下单次数\",\n" +
                "        \"id\": \"order_ct\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"下单金额\",\n" +
                "        \"id\": \"order_amount\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": [");
        for (int i = 0; i< courseOrderCnt.size() ;i++){
            TradeCt tradeCt = courseOrderCnt.get(i);
            sb.append("\n{" +
                    "        \"page_id\": \""+ StringUtils.replace(tradeCt.getName(),"\\","")+"\",\n" +
                    "        \"u_ct\": \""+tradeCt.getUCt()+"\",\n" +
                    "        \"order_ct\": \""+tradeCt.getOrderCt()+"\",\n" +
                    "        \"order_amount\": \""+ tradeCt.getOrderAmount() +"\"\n" +
                    "      }");
            if(i< courseOrderCnt.size() -1){
                sb.append(",");
            }
        }
        sb.append("]}}");

        return sb.toString();
    }

    @RequestMapping("/category")
    public String getCategoryOrderCnt(@RequestParam(value = "date", defaultValue = "1")Integer date){
        if(date == 1){
            date = DateUtil.now();
        }

        List<TradeCt> categoryOrderCnt = courseTradeCountService.getCategoryOrderCnt(date);
        StringBuilder sb = new StringBuilder("  {\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"页面\",\n" +
                "        \"id\": \"page_id\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"下单人数\",\n" +
                "        \"id\": \"u_ct\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"下单次数\",\n" +
                "        \"id\": \"order_ct\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"下单金额\",\n" +
                "        \"id\": \"order_amount\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": [");
        for (int i = 0; i< categoryOrderCnt.size() ;i++){
            TradeCt tradeCt = categoryOrderCnt.get(i);
            sb.append("{\n" +
                    "        \"page_id\": \""+ tradeCt.getName()+"\",\n" +
                    "        \"u_ct\": \""+tradeCt.getUCt()+"\",\n" +
                    "        \"order_ct\": \""+tradeCt.getOrderCt()+"\",\n" +
                    "        \"order_amount\": \""+ tradeCt.getOrderAmount() +"\"\n" +
                    "      }");
            if(i< categoryOrderCnt.size() -1){
                sb.append(",");
            }
        }
        sb.append("]}}");

        return sb.toString();
    }
}
